import * as url from '../../src/api'
import axios from '../http'
import http from 'axios'
import { setLocalForage } from '../utils/localForage'

// 验证手机号是否存在
export function verifyTel(tel) {
  const obj = {
    tel: tel
  }
  return axios({
    url: url.USERVERIFYNAME,
    method: 'post',
    data: obj
  })
}
// 验证账号是否存在
export function verifyAccount(account) {
  const obj = {
    account: account
  }
  return axios({
    url: url.USERVERIFYACCOUNT,
    method: 'post',
    data: obj
  })
}
// 获取验证码
export function getCode(tel) {
  const obj = {
    tel: tel
  }
  return axios({
    url: url.USERGETCODE,
    method: 'post',
    data: obj
  })
}
// 手机号验证码注册
export function registerByCode(tel, code) {
  const obj = {
    tel: tel,
    code: code
  }
  return axios({
    url: url.USERREGISTERBYCODE,
    method: 'post',
    data: obj
  })
}
// 账号密码注册
export function registerByPwd(account, password) {
  const obj = {
    account: account,
    password: password
  }
  return axios({
    url: url.USERREGISTERBYPWD,
    method: 'post',
    data: obj
  })
}
// 手机验证码登陆
export function loginByCode(tel, code) {
  const obj = {
    tel: tel,
    code: code
  }
  return axios({
    url: url.USERLOGINBYCODE,
    method: 'post',
    data: obj
  })
}
// 账号密码登陆
export function loginByPwd(account, password) {
  const obj = {
    account: account,
    password: password
  }
  return axios({
    url: url.USERLOGINBYPWD,
    method: 'post',
    data: obj
  })
}
// export function flatList() {
//   return axios({
//     method: 'get',
//     url: `${process.env.VUE_APP_BOOK_URL}/book/flat-list`
//   })
// }

export function home() {
  return axios({
    url: url.BOOKHOME,
    method: 'get'
  })
  // .then(data => {
  //   console.log(data)
  // }).catch(err => {
  //   console.log(err)
  // })
}
// 获取书籍详情
export function detail(fileName) {
  const obj = {
    fileName: fileName
  }
  return axios({
    method: 'post',
    url: url.BOOKDETAIL,
    // 传入一个参数
    data: obj
  })
}

// 查看全部获取到所有的分类电子书的数据
export function list() {
  return axios({
    method: 'get',
    url: url.BOOKLIST
  })
}

// 获取到所有的解压（未分类）电子书的数据
export function flatList() {
  return axios({
    method: 'get',
    url: url.BOOKFLATLIST
  })
}
// 增加书签
export function addMark(fileName, base, cfi, text) {
  const obj = {
    fileName: fileName,
    base: base,
    cfi: cfi,
    text: text
  }
  console.log('params', obj)
  return axios({
    method: 'post',
    url: url.BOOKADDMARK,
    data: obj
  })
}
// 删除书签
export function deleteMark(fileName, id) {
  const obj = {
    fileName: fileName,
    m_id: id
  }
  return axios({
    method: 'post',
    url: url.BOOKDELETEMART,
    data: obj
  })
}
// 查询书签
export function markList(fileName) {
  const obj = {
    fileName: fileName
  }
  return axios({
    method: 'post',
    url: url.BOOKMARKLIST,
    data: obj
  })
}

export function shelf() {
  return axios({
    method: 'get',
    url: url.BOOKSHELF
  })
}

// 下载电子书的API
export function download(book, onSuccess, onError, onProgress) {
  // 第一步做参数的替换
  // 用户如果只传了三个参数的时候,将第三个参数赋给第四个参数
  if (onProgress == null) {
    onProgress = onError
    onError = null
  }
  // axios的create()方法会返回我们一个function，我们调用这个function，这个function就是get(),通过这个实例的get()方法来实现请求
  return http.create({
    baseURL: process.env.VUE_APP_EPUB_URL,
    method: 'get',
    // 电子书是blob对象,定义responseType为blob对象
    responseType: 'blob',
    // 超时时间，下载3分钟后就认为超时了
    timeout: 180 * 1000,
    // 监听下载时候的进度变化的属性，进度计算
    onDownloadProgress: progressEvent => {
      if (onProgress) onProgress(progressEvent)
    }
    // /调用get方法来获取下载电子书了，电子书路径，因为提供了baseurl所以我么去拼的这路径
  }).get(`${book.categoryText}/${book.fileName}.epub`)
    .then(res => {
      console.log(res)
      // 获取到blob对象
      // if (onSuccess) onSuccess(res)
      const blob = new Blob([res.data])
      // 设置LocalForage的值
      setLocalForage(book.fileName, blob,
        () => onSuccess(book),
        err => onError(err))
    }).catch(err => {
      if (onError) onError(err)
    })
}
// 为了后端排序书架
export function setReadLog(fileName, endReadTime, endProgress) {
  const obj = {
    fileName,
    time: endReadTime,
    percent: endProgress
  }
  return axios({
    method: 'post',
    url: url.SETREADLOG,
    data: obj
  })
}

// 获取个人信息
export function getSelfInfo() {
  return axios({
    method: 'post',
    url: url.USERGETSELFINFO
  })
}

// 设置个人信息
export function setSelfInfo(nickname, password, account, tel) {
  const obj = {
    nickname: nickname,
    password: password,
    tel,
    account
  }
  return axios({
    method: 'post',
    url: url.USERSETSELFINFO,
    data: obj
  })
}

// 添加书籍到书架列表
export function addToShelf(fileName, group) {
  const obj = {
    fileName: fileName,
    group: group
  }
  return axios({
    method: 'post',
    url: url.ADDTOSHELF,
    data: obj
  })
}

// 从书架中删除书籍
export function removeFromShelf(fileNames) {
  const obj = {
    fileNames
  }
  return axios({
    method: 'post',
    url: url.REMOVEFROMSHELF,
    data: obj
  })
}

// 获取书架阅读记录列表
export function getReadLogList() {
  return axios({
    method: 'post',
    url: url.GETREADLOGLIST
  })
}

// 加入分组，移动书籍到分组
export function changeGroup(group, fileNames) {
  const obj = {
    group,
    fileNames
  }
  return axios({
    method: 'post',
    url: url.BOOKCHANGEGROUP,
    data: obj
  })
}

// 获取书架分组列表
export function getGroupList() {
  return axios({
    method: 'post',
    url: url.GETGROUPLIST
  })
}

// 修改分组名  groupName, groupId,
export function updateGroupName(groupId, groupName) {
  const obj = {
    groupId,
    groupName
  }
  return axios({
    url: url.UPDATEGROUP,
    method: 'post',
    data: obj
  })
}

// 删除分组 groupId
export function deleteGroup(groupId) {
  const obj = {
    groupId
  }
  return axios({
    url: url.DELETEGROUP,
    method: 'post',
    data: obj
  })
}

// 获取语音信息
export function getSpeaking(text, fileName) {
  const obj = {
    text,
    fileName
  }
  return axios({
    method: 'post',
    url: url.BOOKVOICE,
    data: obj
  })
}
// 数据分析
export function statics() {
  return axios({
    url: url.STATICS,
    method: 'post'
  })
}
